﻿
function returnValue(xmlatten, i, name) {
    var val = "";
    var eleNode = xmlatten[i].getElementsByTagName(name);
    if (eleNode) {
        if (eleNode[0]) {
            if (eleNode[0].childNodes[0]) {
                val = eleNode[0].childNodes[0].nodeValue;
            }
        }
    }
    return val;
}
function stateChanged() {
    if (xmlhttp.readyState == 4) {
        if (xmlhttp.responseText.indexOf("|", 0) > 0) {
            var arr = xmlhttp.responseText.split("|");
            document.getElementById(receiveid).innerHTML = arr[1];
            if (arr[0] == "sort") {
                sort();
            }
        } else if (xmlhttp.responseText.indexOf("@", 0) > 0) {
            var arr = xmlhttp.responseText.split("@");
            document.getElementById(receiveid).innerHTML = arr[0];
            if (arr[1] == "rplc") {
                senddata('Rpt_nAttendance', 'T=ReplaceSubs' + collectdata(), 'rplceSubs')
            }
        } else if (xmlhttp.responseText == "1") {
            showattendence();
        }
        else {
            document.getElementById(receiveid).innerHTML = xmlhttp.responseText;
            showOfDiv(receiveid);
        }
    } else {
        document.getElementById(receiveid).innerHTML = "<div align='center'><img src='images/loading.gif' /></div>";
    }
}
function showattendence() {
    var strUrl = "xls/studentrecord" + document.getElementById("sesuserid").value + ".xml"
    var strStatusUrl = "xls/studentstatus" + document.getElementById("sesuserid").value + ".xml"
    $.ajax({ type: "GET",
        url: strUrl,
        datatype: "xml",
        success: function (xml) {
            if (document.getElementById('type').value == 2) {
                var givenDate = new Date(getDate1(document.getElementById('txtdatefrom').value));
                var todate = new Date(getDate1(document.getElementById('txtdateto').value));
            }
            else {
                if (document.getElementById("subject").checked) {
                    var getdt = document.getElementById("actitle");

                    var drpcl = getdt.options[getdt.selectedIndex].text;

                    var givenDate = new Date(getDate1(drpcl.substring(0, drpcl.lastIndexOf("-") - 1)));

                    var dt = document.getElementById('Month').value;
                    var _tempdate = new Date(getDaysInMonth(getDate1(dt), ""));
                    var todate = _tempdate;
                } else {
                    var dt = document.getElementById('Month').value;
                    var givenDate = new Date(getDate1(dt));
                    var todate = new Date(getDaysInMonth(givenDate, ""));
                }
            }
            $.ajax({ type: "GET",
                url: strStatusUrl,
                datatype: "xml",
                success: function (status) {
                    var addid = new Array();
                    var sname = new Array();
                    var rollno = new Array();
                    var addno = new Array();
                    var subjectid = new Array();
                    var subjectname = new Array();
                    var xml_student = xml.getElementsByTagName("xmlstudent");
                    for (var i = 0; i < xml_student.length; i++) {
                        addid.push(returnValue(xml_student, i, "ADDID"));
                        sname.push(returnValue(xml_student, i, "STUDENTNAME"));
                        rollno.push(returnValue(xml_student, i, "ROLLNO"));
                        addno.push(returnValue(xml_student, i, "ADDNO"));
                        subjectid.push(returnValue(xml_student, i, "SUBJECTID"));
                        subjectname.push(returnValue(xml_student, i, "SUBJECTTITLE"));
                    }
                    var _mTable = document.createElement("table");
                    _mTable.setAttribute("cellpadding", "0px");
                    _mTable.setAttribute("cellspacing", "5px");
                    _mTable.setAttribute("width", "100%");
                    _mTable.setAttribute("border", "1px");
                    _mTable.setAttribute("id", "aTable");
                    var elediv = document.getElementById("javascriptcontent");
                    var t_print = document.getElementById("t_print");
                    var b_print = document.getElementById("b_print");
                    elediv.innerHTML = '';
                    var len = 0;
                    append_printBtn(t_print);
                    append_printBtn(b_print);
                    if (document.getElementById("subject").checked) {
                        var counter1 = 0;
                        var row = _mTable.insertRow(len);
                        var cell = row.insertCell(0);
                        var _temdate = getDaysInMonth(givenDate, todate);
                        studentattendence_per(givenDate, todate, addid, (givenDate.getMonth() + 1), sname, rollno, addno, status, cell, (todate.getMonth() + 1), subjectid, subjectname);
                        if (elediv) {
                            elediv.appendChild(_mTable);
                        }
                    }
                    else {
                        var count = 0;
                        while (givenDate <= todate) {
                            var row = _mTable.insertRow(len);
                            var cell = row.insertCell(0);
                            var _temdate = getDaysInMonth(givenDate, todate);
                            count = parseInt(count) + 1;
                            if (elediv) {
                                elediv.appendChild(_mTable);
                            }
                            displaytable(givenDate, _temdate, addid, (givenDate.getMonth() + 1), sname, rollno, addno, status, cell, subjectid, subjectname, count);
                            _temdate = addDays(_temdate);
                            givenDate = _temdate;
                            len = parseInt(len) + 1;

                        }
                    }

                }
            })
        }
    })
}
function append_printBtn(_pelement) {
    var el = document.createElement('div');
    el.innerHTML = "<center><input type='button' value='Print' onclick='print();' /></center>"
    _pelement.appendChild(el);
}
function studentattendence_per(fromdate, todate, addid, month, sname, rollno, addno, preStatus, cell, tem_month,subjectid,subjectname) {
    var _table = document.createElement("table");
    var len = _table.rows.length;
    var row = _table.insertRow(len);
    var dat = new Number(DateDiff(fromdate, todate));
    var str = "";
    var givenDate = new Date(fromdate);
    while (givenDate <= todate) {
        if (document.getElementById("subject").checked == true && ((givenDate.getMonth() + 1) == tem_month)) {
            _table.setAttribute("cellpadding", "0px");
            _table.setAttribute("cellspacing", "0px");
            _table.setAttribute("width", "100%");
            _table.setAttribute("border", "1px");
            _table.setAttribute("class", "tbElist");
            _table.setAttribute("style", "border-collapse:collapse; table-layout:auto;border-color:#000");
            str += "<th colspan=" + (parseInt(dat) + 9) + " ><center style='font-size:20px;font-weight:bold'>Attendance Register Upto month of <u>" + getmonthName(givenDate) + "</u></center> </th>";
            row.innerHTML = str;
            var len = _table.rows.length;
            var row = _table.insertRow(len);
            str = "<th>S.No.</th><th>Roll No.</th><th>Student Name</th><th>Add No.</th><th>Subject Name</th>";
            var _fromdate = fromdate;
            str += "<th>Total Present</th><th>Total Absent</th><th>Total Leave</th><th>Total Lecture</th><th>Percentage</th>";
        }
        row.innerHTML = str;
        if (addid.length > 0) {
            var count = 0;
            for (var i = 0; i < addid.length; i++) {
                var len = _table.rows.length;
                var row = _table.insertRow(len);
                var pres = 0
                var apse = 0;
                var leav = 0;
                var total = 0;
                var _stempdate = new Date(_fromdate);
                for (var j = 0; j <= parseInt(dat); j++) {
                    var d = getDayName(_stempdate);
                    var tagname = "d_" + addid[i] + "_" + (parseInt(_stempdate.getMonth()) + 1) + "_" + _stempdate.getDate() + "_" + _stempdate.getFullYear()+"_"+subjectid[i];
                    var _status = preStatus.getElementsByTagName(tagname);
                    if (_status) {
                        for (var k = 0; k < _status.length; k++) {
                            if (_status[k]) {
                                if (_status[k].childNodes[0]) {
                                    if (_status[k].childNodes[0].nodeValue == "A") {
                                        apse += 1;
                                    }
                                    else if (_status[k].childNodes[0].nodeValue == "L") {
                                        leav += 1;
                                    }
                                    else if (_status[k].childNodes[0].nodeValue == "P") {
                                        pres += 1;
                                    }
                                    total = apse + pres + leav;
                                }
                            }
                        }
                    }
                    _stempdate = new Date(addDays(_stempdate));
                }

                if (document.getElementById("subject").checked == true && ((givenDate.getMonth() + 1) == tem_month)) {
                    var per = 0;
                    per = (Math.floor(parseFloat(pres) * 100) / (parseFloat(pres) + parseFloat(apse) + parseFloat(leav)) * 100) / 100;
                    var _style = "style='";
                    if (parseFloat(per) < 75) {
                        _style += "color:red";
                    }
                    if (addid[i] != addid[i - 1]) {
                        _style += "'";
                        count = parseInt(count) + 1;
                        str = "<td " + _style + ">" + count + "</td><td " + _style + ">" + rollno[i] + "</td><td " + _style + ">" + sname[i] + "</td><td " + _style + ">" + addno[i] + "</td>";
                    }
                    else {

                        str = "<td " + _style + "border-top:1px solid #DAEEF6'></td><td " + _style + "border-top:1px solid #DAEEF6'></td><td " + _style + "border-top:1px solid #DAEEF6'></td><td " + _style + "border-top:1px solid #DAEEF6'></td>";
                    }
                    _style += "'";
                    str += "<td>" + subjectname[i] + "</td><td " + _style + ">" + pres + "</td><td " + _style + ">" + apse + "</td><td " + _style + ">" + leav + "</td><td " + _style + ">" + total + "</td><td " + _style + ">" + Math.floor(per * 100) / 100 + "</td>"
                }
                row.innerHTML = str;
            }
        }
        else {
            if (document.getElementById("subject").checked == true && ((givenDate.getMonth() + 1) == tem_month)) {
                var len = _table.rows.length;
                var row = _table.insertRow(len);
                str = "<td colspan=" + (parseInt(dat) + 9) + " style='text-align:center'>No Record Found.</td>"
                row.innerHTML = str;
            }
        }
        cell.appendChild(_table);
        var _temdate = getDaysInMonth(givenDate, todate);
        _temdate = addDays(_temdate);
        givenDate = _temdate;
        len = parseInt(len) + 1;
    }
}
///////

// _mTable += "</table>"
function getmonthName(fromdate) {
    var m = new Date(fromdate);
    var month = new Array(12);
    month[0] = "January";
    month[1] = "Februaury";
    month[2] = "March";
    month[3] = "April";
    month[4] = "May";
    month[5] = "June";
    month[6] = "July";
    month[7] = "August";
    month[8] = "September";
    month[9] = "October";
    month[10] = "November";
    month[11] = "December";
    var x = month[m.getMonth()];
    return x;
}

function getDayName(dat) {
    var d = new Date(dat);
    var weekday = new Array(7);
    weekday[0] = "Sun";
    weekday[1] = "Mon";
    weekday[2] = "Tue";
    weekday[3] = "Wed";
    weekday[4] = "Thu";
    weekday[5] = "Fri";
    weekday[6] = "Sat";
    var x = weekday[d.getDay()];
    return x;
}
function getDate1(date) {
    var rDate = date;
    if (date.indexOf("/") > 0) {
        var _tDate = date.split("/");
        rDate = _tDate[1] + "/" + _tDate[0] + "/" + _tDate[2];
    }
    return rDate;
}
function displaytable(fromdate, todate, addid, month, sname, rollno, addno, preStatus, cell,subjectid,subjectname,cnt) {
    var dat = new Number(DateDiff(fromdate, todate));
    var _table = document.createElement("table");
    _table.setAttribute("cellpadding", "0px");
    _table.setAttribute("cellspacing", "0px");
    _table.setAttribute("width", "100%");
    _table.setAttribute("border", "1px");
    _table.setAttribute("class", "tbElist");
    _table.setAttribute("style", "border-collapse:collapse;table-layout:auto;border-color:#000");
    _table.setAttribute("id", "atten" + cnt);
    
//    var id = "atten" + cnt;
//    var ele = document.getElementById(id);
    var len = _table.rows.length;
    var row = _table.insertRow(len);
    var str = [];
    str.push("<th colspan=" + (parseInt(dat) + 9) + " ><center style='font-size:20px;font-weight:bold'>Attendance Register for the month of <u>" + getmonthName(fromdate) + "</u></center> </th>");
    row.innerHTML = str.join();
    var len = _table.rows.length;
    var row = _table.insertRow(len);
    str = [];
    str.push("<th>S.No.</th><th>Roll No.</th><th>Student Name</th><th>Add No.</th><th>Subject Name</th>");
    var _fromdate = fromdate;
    for (var i = 0; i <= parseInt(dat); i++) {
        var d = getDayName(fromdate);
        str.push("");
        var _tempFormDate = new Date(addDays(fromdate));
        if (d == "Sun") {
            str.push("<th style='width:5px; background-color:gray'>" + _tempFormDate.getUTCDate() + "<br/>" + d + "</th>");
        }
        else {
           str.push("<th style='width:5px'>" + _tempFormDate.getUTCDate() + "<br/>" + d + "</th>");
        }
        fromdate = addDays(fromdate);
    }
    str.push("<th>TP</th><th>TA</th><th>TL</th>");
    row.innerHTML = str.join(""); 
    if (addid.length > 0) {
        var count = 0
        for (var i = 0; i < addid.length; i++) {
            var len = _table.rows.length;
            var row = _table.insertRow(len);
            var pres = 0
            var apse = 0;
            var leav = 0;
            var _stempdate = new Date(_fromdate);
            var str=[];
            if (addid[i] != addid[i - 1]) {
                count = parseInt(count) + 1;
                str.push("<td>" + count + "</td><td>" + rollno[i] + "</td><td>" + sname[i] + "</td><td>" + addno[i] + "</td>");
            }
            else {
                str.push("<td style='border-top:1px solid #DAEEF6;'></td><td style='border-top:1px solid #DAEEF6'></td><td style='border-top:1px solid #DAEEF6'></td><td style='border-top:1px solid #DAEEF6'></td>");
            }
            str.push("<td>" + subjectname[i] + "</td>");
            for (var j = 0; j <= parseInt(dat); j++) {
                var d = getDayName(_stempdate);
                if (d == "Sun") {
                    str.push("<td style='background-color:gray'>");
                } else { str.push("<td>"); }
                var tagname = "d_" + addid[i] + "_" + (parseInt(_stempdate.getMonth()) + 1) + "_" + _stempdate.getDate() + "_" + _stempdate.getFullYear() + "_" + subjectid[i];
                var _status = preStatus.getElementsByTagName(tagname);
                if (_status) {
                    for (var k = 0; k < _status.length; k++) {
                        if (_status[k]) {
                            if (_status[k].childNodes[0]) {
                                if (_status[k].childNodes[0].nodeValue == "A") {
                                    str.push('<span style="border:1px solid red; -moz-border-radius:50px;-webkit-border-radius:50px;border-radius:50px; padding:2px; text-align:center; vertical-align:middle">' + _status[0].childNodes[0].nodeValue + '</span>');
                                    apse += 1;
                                }
                                else if (_status[k].childNodes[0].nodeValue == "L") {
                                    leav += 1;
                                }
                                else if (_status[k].childNodes[0].nodeValue == "P") {
                                    pres += 1;
                                    str.push('<span>' + _status[0].childNodes[0].nodeValue + '</span>');
                                }
                                else {
                                    str.push('<span>' + _status[0].childNodes[0].nodeValue + '</span>');
                                }
                            }
                            else {
                                str.push("");
                            }
                        }
                        else {
                            str.push("");
                        }
                    }
                }
                else {
                    str.push("");
                }
                str.push("</td>");
                _stempdate = new Date(addDays(_stempdate));
            }
            str.push("<td>" + pres + "</td><td>" + apse + "</td><td>" + leav + "</td>");
            row.innerHTML = str.join(""); ;
           // spend_time(30000);
            //alert("enter");
        }
    }
    else {
        var len = _table.rows.length;
        var row = _table.insertRow(len);
        str = [];
        str.push("<td colspan=" + (parseInt(dat) + 9) + " style='text-align:center'>No Record Found.</td>");
        row.innerHTML = str.join("");
    }
    cell.appendChild(_table);
}
function addDays(dat) {
    var addDays = "1";
    var date = new Date(dat);
    return new Date(date.getTime() + (addDays * 24 * 60 * 60 * 1000));
}
//give diff between 2 date
function DateDiff(dat1, dat2) {
    var date1 = new Date(dat1);
    var date2 = new Date(dat2);

    var today = new Date(date1.getFullYear(), date1.getMonth(), date1.getDate())
    var frmdate = new Date(date2.getFullYear(), date2.getMonth(), date2.getDate())
    var day = 1000 * 60 * 60 * 24;

    return ((frmdate.getTime() - today.getTime()) / (day));

}
////////get last day in  month
function getDaysInMonth(_fromData, _toDate) {
    var fDate = new Date(_fromData);
    if (_toDate != "") {
        var tdate = new Date(_toDate);
    }
    var m = new Number(fDate.getMonth());
    var y = new Number(fDate.getYear());
    var arrMonthLastDay = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
    var lDay = arrMonthLastDay[m];
    if ((y % 400 == 0 || (y % 100 != 0 && y % 4 == 0))) {
        if (m == 1) {
            lDay = parseInt(lDay) + 1;
        }
    }
    var _temDate = (m + 1) + "/" + lDay + "/" + fDate.getFullYear();
    var ldate = new Date(_temDate);
    if (_toDate != "") {
        if (ldate > tdate) {
            ldate = tdate;
        }
    }
    return ldate;
}
function hideunhide2(val) {
    if (val == 1) {
        document.getElementById('searchtext').innerHTML = 'Student Name'
    } else if (val == 2) {
        document.getElementById('searchtext').innerHTML = 'Add No.'
    }
    else if (val == 3) {
        document.getElementById('searchtext').innerHTML = 'Roll No.'
    }
    else {
        document.getElementById('searchtext').innerHTML = 'University Roll No.'
    }
}
function hideunhide(val) {
    if (val == 1) {
        document.getElementById('SearchMonth').className = 'inline';
        document.getElementById('SearchDate').className = 'none';
        document.getElementById('txtdatefrom').title = 'isDate';
        document.getElementById('txtdateto').title = 'isDate';
    }
    else {
        document.getElementById('SearchMonth').className = 'none';
        document.getElementById('SearchDate').className = 'inline';
        document.getElementById('txtdatefrom').title = 'RisDate';
        document.getElementById('txtdateto').title = 'RisDate';
    }
}

function collectdata() {
    var frm = document.forms[0];
    var st = "";
    var sel = document.getElementsByTagName("select");
    for (var i = 0; i < sel.length; i++) {
        st += "&" + sel[i].name + "=" + sel[i].value;
    }
    var sel = document.getElementsByTagName("textarea");
    for (var i = 0; i < sel.length; i++) {
        st += "&" + sel[i].name + "=" + sel[i].value;
    }
    for (var i = 0; i < frm.length; i++) {
        if (frm[i].type == 'text' || frm[i].type == 'password')
            st += "&" + frm[i].name + "=" + frm[i].value;
    }
    return st;
}
function go() {
    clearPrint();
    document.getElementById("javascriptcontent").innerHTML = " ";
    if (document.getElementById('type').value == 2) {
        var givenDate = document.getElementById('txtdatefrom').value;
        var todate = document.getElementById('txtdateto').value;
    }
    else {
        if (document.getElementById("subject").checked) {
            var getdt = document.getElementById("actitle");
            var drpcl = getdt.options[getdt.selectedIndex].text;
            var givenDate = drpcl.substring(0, drpcl.lastIndexOf("-") - 1);
            var dt = document.getElementById('Month').value;
            var _tempdate = new Date(getDaysInMonth(getDate1(dt), ""));
            var todate = (parseInt(_tempdate.getUTCDate()) + 1) + "/" + (parseInt(_tempdate.getMonth()) + 1) + "/" + _tempdate.getFullYear();

        } else {
            var dt = document.getElementById('Month').value;
            var givenDate = dt;
            var _tempdate = new Date(getDaysInMonth(getDate1(dt), ""));
            var todate = (parseInt(_tempdate.getUTCDate()) + 1) + "/" + (parseInt(_tempdate.getMonth()) + 1) + "/" + _tempdate.getFullYear();
        }
    }
    if (validate()) {
        senddata('Rpt_nAttendance', 'T=Filtercheck&rtype=' + getValue('rtype') + collectdata() + '&fromdate=' + givenDate + '&todate=' + todate, "javascriptcontent");
    }
}
function gofilter1() {
    if (document.getElementById("locationid").value)
    { senddata('Rpt_nAttendance', 'T=AcademicYear&locationid=' + document.getElementById("locationid").value, 'filtercontent'); }
    else { alert("Select Location Id."); } 
}
function gofilter2() { senddata('Rpt_nAttendance', 'T=Course' + collectdata(), 'Course'); }
getValue = function (name) {
    var ele = document.getElementsByName(name);
    for (var i = 0; i < ele.length; i++) {
        if (ele[i].checked == true) {
            return i;
        }
    }
}
hide = function (val) {
    if (val == 0) {
       // document.getElementById('stu').style.display = "block";
       // document.getElementById('pstu').style.display = "none";
        document.getElementById('SubjectTitle').setAttribute("title", "Riscmb");
        document.getElementById('sSubjectTitle').innerHTML = '*';
        document.getElementById('tLecNo').style.display = "block"

    }
    else if (val == 1 || val == 3) {
      //  document.getElementById('stu').style.display = "block";
        //document.getElementById('pstu').style.display = "none";
        document.getElementById('SubjectTitle').setAttribute("title", "");
        document.getElementById('sSubjectTitle').innerHTML = '';
        document.getElementById('tLecNo').style.display = "none"

    }
    else {
      //  document.getElementById('stu').style.display = "none";
       // document.getElementById('pstu').style.display = "block";
        document.getElementById('SubjectTitle').setAttribute("title", "");
        document.getElementById('sSubjectTitle').innerHTML = '';
        document.getElementById('tLecNo').style.display = "none"

    }
}
function print() {
    var ele = document.getElementById('javascriptcontent');
    if (ele) {
        var course = " ";
        var sem = " ";
        var ac = " ";
        if (document.getElementById('courseTitle')) {
            var el_course = document.getElementById('courseTitle');
            if (parseInt(el_course.value) != 0) {
                course = el_course.options[el_course.selectedIndex].text;
            }
        }
        if (document.getElementById('Semname')) {
            var el_course = document.getElementById('Semname');
            if (parseInt(el_course.value) != 0) {
                sem = el_course.options[el_course.selectedIndex].text;
            }
        }
        if (document.getElementById('actitle')) {
            var el_course = document.getElementById('actitle');
            if (parseInt(el_course.value) != 0) {
                ac = el_course.options[el_course.selectedIndex].text;
            }
        }
        var wn = window.open('', "Attendance Report", 'height=400px; width=400px');
        var _content = "<html xmlns='http://www.w3.org/1999/xhtml'><head><title>Campus Management Solution</title><link rel='Stylesheet' type='text/css' href='StyleSheet.css'  /></head><body>";
        _content += "<div style='width:100%;height:70px'><div style='float:left;width:29%;height:50px'>"
        _content += "<table cellpadding='2' cellspacing='0' style='width:100%;' border='0'>"
        _content += "<tr><td style='border-bottom:1px dashed #000'>Course :</td><td style='border-bottom:1px dashed #000'><b>" + course + "</b></td></tr><tr><td style='border-bottom:1px dashed #000'>Semester :</td><td style='border-bottom:1px dashed #000'><b>" + sem + "</b></td></tr><tr><td style='border-bottom:1px dashed #000'>Academic Year :</td><td style='border-bottom:1px dashed #000'><b>" + ac + "</b></td></tr></table></div>";
        _content += "<div style='float:left;width:30%;height:70px'></div>";
        _content += "<div style='float:left;width:40%;height:20px;text-align:right;padding-top:30px'><span style='font-size:18px;font-weight:bold;'>" + document.getElementById('seslocname').value + "</span></div>";
        _content += "</div>";
        _content += ele.innerHTML;
        _content += "</body></html>";
        wn.document.write(_content);
        wn.print();
        //wn.close();
    }
}
function clearPrint() {
    document.getElementById('t_print').innerHTML = '';
    document.getElementById('b_print').innerHTML = '';
}